import webapp2
from google.appengine.ext import db
from category import Category

class IndexRequestHandler(webapp2.RequestHandler):
  def get(self):
    cats = Category.all().fetch(100)
    
    images = ""
    i = 0
    for cat in cats:
      if i % 4 == 0:
        images += "<tr>"
      images += ('<td colspan="2"><br/><br/><img style="width:100px;height:100px" id="menu" src="/imagesDB/%s" alt="%s"></td>' % (str(cat.image.key()), str(cat.name)))
      if i % 4 == 3:
        images += "</tr>"
      i += 1
  
    resp = """<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      <html>
      <head>
      <meta http-equiv="Content-Type" content="text/html; windows-1255">
      <link rel="stylesheet" href="/pages/css/css.css">
      <script language="javascript"> 
      $('input#myinput').autoGrowInput({
          comfortZone: 50,
          minWidth: 200,
          maxWidth: 2000
      });
      </script> 
      
      
      <title>Know in advance</title>
      </head>
      <body>
      <div id="content">
      <table width="100%%" border="0">
      <tr>
      <td colspan="8">
      <br/>
      <br/>
      <br/>
      <br/>
      </td>
      </tr>
      <tr>
      <td colspan="8">
      
      
      <div id="content">
      <center>
      <table border="0">
      <tr>
      <td>
      <img style="margin-top:0px;width:65px;height:65px" src="/pages/images/logo.jpg">
      </td>
      <td>
      <input id="myinput" name="myinput" style="margin-top:10px;width:600px;height:50px" /> 
      </td>
      </tr>
      <tr>
      <td colspan="2"><div dir="rtl">search
      </div></td>
      </tr>
      </table>
      </center>
      </div>
      </tr>
      %(images)s
      </table>
      </div>
      </body>
      </html>
    """ % {'images': images}
    self.response.out.write(resp)
  
  def __findImageByKey(self, id):
    try:
      return db.get(id)
    except:
      return None

